package com.example.testapp.activity.view.bitmap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.example.testapp.activity.activity.meact.PictureActivity;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.HashMap;

/**
 * Created by l on 2016/7/4.
 */
public class BitmapCache extends Activity{
    public final String TAG = getClass().getSimpleName();

    private Handler mHandler = new Handler();
    private HashMap<String, SoftReference<Bitmap>>  imageCache = new HashMap<String, SoftReference<Bitmap>>();

    public void displayBitmap(final ImageView imageView, final String thumbPath, final String sourcePath, final ImageCallback callback) {
        if(TextUtils.isEmpty(thumbPath) && TextUtils.isEmpty(sourcePath)){
            Log.e(TAG, "no paths pass in");
            return;
        }
        final String path;
        final boolean isThumbPath;
        if(!TextUtils.isEmpty(thumbPath)){
            path = thumbPath;
            isThumbPath = true;
        }else if(!TextUtils.isEmpty(sourcePath)){
            path = sourcePath;
            isThumbPath = false;
        }else{
            return;
        }

        if(imageCache.containsKey(path)){
            SoftReference<Bitmap> reference = imageCache.get(path);
            Bitmap bitmap = reference.get();
            if(bitmap != null){
                if(callback != null){
                    callback.imageLoad(imageView, bitmap, sourcePath);
                }
                imageView.setImageBitmap(bitmap);
                Log.d(TAG, "hit cache");
                return;
            }
        }

        imageView.setImageBitmap(null);

        new Thread(){
            Bitmap thumb;

            @Override
            public void run() {
                try{
                    if (isThumbPath){
                        thumb = BitmapFactory.decodeFile(thumbPath);
                        if(thumb == null){
                            thumb = revitionImageSize(sourcePath);
                        }
                    }else{
                        thumb = revitionImageSize(sourcePath);
                    }
                }catch (Exception e){

                }
                if(thumb == null){
                   thumb = PictureActivity.bitmap;
                }
                Log.e(TAG, "-------thumb------"+thumb);
                put(path, thumb);

                if(callback != null){
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.imageLoad(imageView, thumb, sourcePath);
                        }
                    });
                }
            }
        }.start();
    }

    private Bitmap revitionImageSize(String path) throws IOException {
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, options);
        is.close();
        int i = 0;
        Bitmap bitmap = null;
        while (true){
            if((options.outWidth >> i <= 256) && (options.outHeight >> i <= 256)){
                is = new BufferedInputStream(new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(is, null, options);
                break;
            }
            i += 1;
        }
        return bitmap;
    }

    private void put(String path, Bitmap bmp) {
       if(!TextUtils.isEmpty(path) && bmp != null){
           imageCache.put(path, new SoftReference<Bitmap>(bmp));
       }
    }

    public interface ImageCallback{
        public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params);
    }
}
